package com.bothin.experiment4.service.impl;

import com.bothin.experiment4.constant.EmployeeTypeConstant;
import com.bothin.experiment4.enums.EmployeeEnum;
import com.bothin.experiment4.model.Employee;
import com.bothin.experiment4.service.IEmployeeService;
import com.bothin.experiment4.service.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by bothin
 * At 2019/12/15 13:44
 * Description:
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService{

    @Override
    public Employee findById(Object id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> findAll() {
        return employeeMapper.selectAll();
    }

    @Override
    @Transactional
    public void deleteById(Object id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    @Transactional
    public void insert(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    @Override
    public List<Employee> findAllSalesmans() {
        Example example = new Example(Employee.class);
        example.createCriteria().andEqualTo("role",EmployeeEnum.SALESMAN.getName());
        return employeeMapper.selectByExample(example);
    }

    @Override
    public List<Employee> findAllSalesmansUsable() {
        Example example = new Example(Employee.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("role",EmployeeEnum.SALESMAN.getName());
        criteria.andEqualTo("usable", EmployeeTypeConstant.USABLE);
        return employeeMapper.selectByExample(example);
    }

    @Override
    public List<Employee> findAllStockmans() {
        Example example = new Example(Employee.class);
        example.createCriteria().andEqualTo("role",EmployeeEnum.STOCKMAN.getName());
        return employeeMapper.selectByExample(example);
    }

    @Override
    public Employee findByAccount(String account) {
        Example example = new Example(Employee.class);
        example.createCriteria().andEqualTo("account",account);
        List<Employee> employees = employeeMapper.selectByExample(example);
        if (employees.size()==0)
            return null;
        return employees.get(0);
    }

    @Override
    public List<Employee> findSalesmanByKeyword(String key) {
        return employeeMapper.findSalesmanByKeyword("%"+key+"%");
    }

    @Override
    public List<Employee> findStockmanByKeyword(String key) {
        return employeeMapper.findStockmanByKeyword("%"+key+"%");
    }
}
